<?php

/**
 * This is the model class for table "{{tag}}".
 *
 * The followings are the available columns in table '{{tag}}':
 * @property integer $id
 * @property string $name
 * @property integer $frequency
 */
class Tag extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Tag the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function updateFrequency($oldTag,$newTag)
	{
		if($oldTag == '')//This is a new record
		{
			$newTagsArray = explode(',', $newTag);
			foreach($newTagsArray as $item)
			{
				if(self::model()->exists('name=:name',array(':name'=>$item)))
				{
					$this->increaseTagFrequency($item);
				}
				else
				{
					$this->addNewTag($item);
				}
			}
		}
		else //This is an old record
		{
			$oldTagsArray = explode(',', $oldTag);
			$newTagsArray = explode(',', $newTag);
				
			$needAddTags = array_diff($newTagsArray, $oldTagsArray);
			$needSubTags = array_diff($oldTagsArray,$newTagsArray);
			
			foreach($needAddTags as $item)
			{	
				if(trim($item)=='')//if there is a meanless value,ignore it
				{
					continue;
				}
				
				if(self::model()->exists('name=:name',array(':name'=>$item)))
				{
					$this->increaseTagFrequency($item);
				}
				else
				{
					$this->addNewTag($item);
				}
			}
			foreach($needSubTags as $item)
			{
				$this->decreaseTagFrequency($item);
			}
				
		}
	}

	protected function increaseTagFrequency($tagName)
	{
		$tag = self::model()->find('name=:name',array(':name'=>$tagName));
		$tag->frequency++;
		$tag->save();
	}

	protected function addNewTag($tagName)
	{
		$tag = new Tag();
		$tag->name = $tagName;
		$tag->frequency = 1;
		$tag->save();
	}

	protected function decreaseTagFrequency($tagName)
	{
		$tag = self::model()->find('name=:name',array(':name'=>$tagName));
		$tag->frequency--;
		if($tag->frequency == 0)
		{
			$tag->delete();
		}
		else
		{
			$tag->save();
		}
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{tag}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		// 			array('name', 'required'),
		// 			array('frequency', 'numerical', 'integerOnly'=>true),
		// 			array('name', 'length', 'max'=>128),
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, name, frequency', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'frequency' => 'Frequency',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('frequency',$this->frequency);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}